function stackTraceHere(){
    var Exception = Java.use('java.lang.Exception');
    var Log = Java.use('android.util.Log');
    console.log(Log.getStackTraceString(Exception.$new()))
}
var data=null;
var appid = null;

function url() {
    console.log(data)
};

function getAppid(){
    console.log(appid);
}

function jumpInternal(appid,url=null) {
    // 导入Frida的Java模块
    var context = Java.use('android.app.ActivityThread').currentApplication().getApplicationContext();
    var str
    if(url){
        str="alipays://platformapi/startApp?appId="+appid+"&page="+url
    }
    else{
        str="alipays://platformapi/startApp?appId="+appid
    }
    var intent = Java.use('android.content.Intent').$new('android.intent.action.VIEW', Java.use('android.net.Uri').parse(str));
    intent.setFlags(Java.use('android.content.Intent').FLAG_ACTIVITY_NEW_TASK.value)
    context.startActivity(intent);
};
Java.perform(function(){
    var String = Java.use("java.lang.String")
    var c=Java.use("com.alibaba.ariver.app.PageNode")
    c.getPageURI.implementation=function(){
        //stackTraceHere()
        var ret=this.getPageURI()
        if(ret==null) return null
        var tmp=String.$new(ret)
        if(!tmp.equals(data) && tmp.contains(String.$new("#"))){
            data=tmp;
            appid=tmp.split('://')[1].split(".")[0];
        }
        return ret
    }
})

rpc.exports = {
    getUrl: function() {
        if (data === null) {
            return null;
        } else {
            // 调用 toString() 将 Java 字符串转换为 JavaScript 字符串
            return data.toString();
        }
    },
    jump: function(appid, url) {
        Java.perform(function() {
            jumpInternal(appid, url);
        });
    },
    getAppid: function() {
        if (appid === null) {
            return null;
        } else {
            // Return the extracted data
            return appid.toString();
        }
    }
};
